package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1:8080/api/student ---- post
     * {"name":"franxhao","email":"franxhao@163.com","age":18}
     * {"name":"franxhao1","email":"franxhao1@163.com","age":19,"studentCard":{"cardNo":"HQYJ_00001"}}
     * {"name":"franxhao2","email":"franxhao2@163.com","age":20}
     */
   /* @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }*/

    /**
     * 判断是否有唯一标识（id），如果有值就update，没有值则insert新增
     * 127.0.0.1:8080/api/student ---- put
     * {"id":1,"name":"franxhao","email":"franxhao@163.com","age":18}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1:8080/api/student/13 ---- delete
     */
    @DeleteMapping("/student/{id}")
    public Result<Object> deleteStudent(@PathVariable int id) {
        return studentService.deleteStudent(id);
    }

    /**
     * 127.0.0.1:8080/api/student/2 ---- get
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1:8080/api/students ---- get
     */
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /**
     * 127.0.0.1:8080/api/student?name=franxhao1&email=franxhao1@163.com ---- get
     */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name, email);
    }

    /**
     * 127.0.0.1:8080/api/students ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /**
     * 批量操作接口
     * 127.0.0.1:8080/api/students/v2
     * [{"name":"aa","email":"aa@163.com","age":18},
     * {"name":"aa1","email":"aa1@163.com","age":19}]
     */
    @PostMapping(value = "/students/v2", consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<Student> students) {
        return studentService.insertStudents(students);
    }
}
